package com.transaction.simulate.controller;

import com.transaction.common.base.BaseController;
import com.transaction.common.base.PageInfo;
import com.transaction.common.util.ParamsUtil;
import com.transaction.simulate.model.dto.Account;
import com.transaction.simulate.model.dto.AccountStatus;
import com.transaction.simulate.model.dto.ChannelAccount;
import com.transaction.simulate.model.vo.ChannelAccountVo;
import com.transaction.simulate.service.AccountService;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 账户信息
 * @author: dy.yin 2021/8/18 13:45
 */

@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {

    private static Logger LOGGER = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    ExecutorService executorService;


    /**
     * 随机生成用户并入库
     * @Title: generateAccountInfo
     * @author: dy.yin 2021/8/20 9:10
     */
    @PostMapping(value= "/generateAccount")
    public Object generateAccountInfo(@RequestBody Map<String,Object> map){
        Integer num = MapUtils.getInteger(map,"num");
        accountService.generateAccountList(num);
        return retResultVo();
    }

    /**
     * 查询生成的账户
     * @Title: getAccount
     * @author: dy.yin 2021/8/24 10:54
     */
    @GetMapping("/getAccount")
    public Object getAccount(HttpServletRequest request){
        PageInfo pageInfo = ParamsUtil.getPageInfo(request);
        Account account = ParamsUtil.getBeanParam(new Account(),request);

        List<Account> accountList = accountService.getAccount(account,pageInfo);
        pageInfo.setPageResult(accountList);
        return pageInfo;
    }

    /**
     * 删除所有生成的账户信息
     * @Title: deleteAll
     * @author: dy.yin 2022/3/8 15:51
     * @return: {@link Object }
     */
    @PostMapping("/deleteAll")
    public Object deleteAll(){
        accountService.deleteAll();
        return retResultVo();
    }


    /**
     * 生成渠道账户开户信息
     * @Title: generateChannelAccount
     * @author: dy.yin 2021/8/23 13:11
     * @param: [channelCode, transAccountIds]
     * @return: java.lang.Object
     * @throws
     */
    @PostMapping("/generateChannelAccount")
    public Object generateChannelAccount(@RequestBody ChannelAccountVo channelAccountVo){

        List<String> channelCodes = channelAccountVo.getChannelCodes();
        List<Account> transAccounts = channelAccountVo.getTransAccounts();
        String transDate = channelAccountVo.getTransDate();

        executorService = Executors.newFixedThreadPool(channelCodes.size());
        channelCodes.forEach( v->{
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    accountService.generateChannelAccount(v,transDate,transAccounts);
                }
            });
        });
        return retResultVo();
    }

    /**
     * 查询渠道客户信息
     * @Title: getChannelAccount
     * @author: dy.yin 2021/9/10 14:12
     */
    @GetMapping("/getChannelAccount")
    public Object getChannelAccount(HttpServletRequest request){
        PageInfo pageInfo = ParamsUtil.getPageInfo(request);
        ChannelAccount channelAccount = ParamsUtil.getBeanParam(new ChannelAccount(),request);

        List<ChannelAccount> accountList = accountService.getChannelAccount(channelAccount,pageInfo);
        pageInfo.setPageResult(accountList);
        return pageInfo;
    }

    /**
     * 删除所有渠道的账户信息
     * @Title: deleteAll
     * @author: dy.yin 2022/3/8 15:51
     * @return: {@link Object }
     */
    @PostMapping("/deleteChannelAccountAll")
    public Object deleteChannelAccountAll(){
        accountService.deleteChannelAccountAll();
        return retResultVo();
    }

    /**
     * 查询渠道客户产品持仓
     * @Title: getChannelAccountAvailable
     * @author: dy.yin 2021/9/10 14:11
     */
    @GetMapping("/getChannelAccountAvailable")
    public Object getChannelAccountAvailable(HttpServletRequest request){
        PageInfo pageInfo = ParamsUtil.getPageInfo(request);
        ChannelAccountVo channelAccount = ParamsUtil.getBeanParam(new ChannelAccountVo(),request);

        List<ChannelAccountVo> accountReconciliationList = accountService.getChannelAccountAvailable(channelAccount,pageInfo);
        pageInfo.setPageResult(accountReconciliationList);
        return pageInfo;
    }

    /**
     * 查询账户状态
     * @param request
     * @return
     */
    @GetMapping("/getAccountStatus")
    public Object getAccountStatus(HttpServletRequest request){
        PageInfo pageInfo = ParamsUtil.getPageInfo(request);
        AccountStatus accountStatus = ParamsUtil.getBeanParam(new AccountStatus(),request);

        List<AccountStatus> accountStatusList = accountService.getAccountStatus(accountStatus,pageInfo);
        pageInfo.setPageResult(accountStatusList);
        return pageInfo;
    }

    /**
     * 删除账户状态信息
     * @param accountStatus 账户状态信息
     * @return
     */
    @PostMapping("/deleteAllAccountStatus")
    public Object deleteAllAccountStatus(@RequestBody AccountStatus accountStatus){
        accountService.deleteAllAccountStatus(accountStatus);
        return retResultVo();
    }

    /**
     * 修改账户4要素信息
     * @param accountStatus 账户状态信息
     * @return
     */
    @PostMapping("/updateAccountStatus")
    public Object updateAccountStatus(@RequestBody AccountStatus accountStatus){
        accountService.updateAccountStatus(accountStatus);
        return retResultVo();
    }

}
